package com.bzy.service;

import com.bzy.Book;
import com.bzy.BookInfo;
import com.bzy.DataPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;


/**
 * @ Author     : Bzy.
 * @ Date       : 2020/12/28 -- 19:49
 * @ Version:     $ 1.0
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${book.page.size}")
    private Integer pageSize;

    @Value("${book.rest.url}")
    private String bookUrl;

    @Value("${bookInfo.rest.url}")
    private  String  bookInfoUrl;

    @Override
    public DataPage<Book> getAllBook(Integer pageNum) {//1
        String url = String.format("%s?pageNum=%d&pageSize=%d", bookUrl, pageNum, pageSize);//1,3
        System.out.println(url);
        return restTemplate.getForObject(url, DataPage.class);
    }

    @Override
    public List<BookInfo> getAllBookInfo() {
        System.out.println("111111");
        BookInfo[] bookInfos = restTemplate.getForObject(bookInfoUrl, BookInfo[].class);
        System.out.println("222222");
        System.out.println("bookInfos:"+bookInfos);
        return Arrays.asList(bookInfos);
    }

}
